//
//  cityListViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/8/9.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "cityListViewController.h"
#import "cityListTableViewCell.h"
#import "schoolChangeViewController.h"
#import "BMChineseSort.h"
#import "Person.h"

@interface cityListViewController ()<UITableViewDelegate , UITableViewDataSource>{
    
    NSMutableArray<Person *> *dataArray;
}

//排序后的出现过的拼音首字母数组
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的结果数组
@property(nonatomic,strong)NSMutableArray *letterResultArr;

@property(nonatomic,strong) NSMutableDictionary *dataDic;
@property(nonatomic,strong) UITableView *helpTableView;
@property(nonatomic,strong) UILabel *moneyNumLabel;

@end

@implementation cityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self initView];
    [self requestData];
}

#pragma mark ----------------------- requestData ---------------------

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:more_allAddress
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       _dataArr = returnData[@"data"];
                                       NSLog(@"\n\n\n帮助===\n\n\n%@",_dataArr);
                                       
                                       if (_dataArr) {
                                           
                                           [self initView];
                                           
                                       }
                                       
                                       
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ------------------ init -------------------

//加载模拟数据
-(void)loadData{
    NSArray *stringsToSort = [NSArray arrayWithObjects:
                            @"啊",
                            @"李白",@"张三",
                            @"重庆",@"重量",
                            @"调节",@"调用",
                            @"小白",@"小明",@"千珏",
                            @"黄家驹", @"鼠标",@"hello",@"多美丽",@"肯德基",@"##",
                            nil];
    if (_dataArr) {
        
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        for (int i = 0; i < _dataArr.count ; i ++) {
            
            [arr addObject:_dataArr[i][@"area_name"]];
            
        }
        
        stringsToSort = [NSArray arrayWithArray:arr];
        
    }
    NSLog(@"===%@===",stringsToSort);
    //模拟网络请求接收到的数组对象 Person数组
    dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i<[stringsToSort count]; i++) {
        Person *p = [[Person alloc] init];
        p.name = [stringsToSort objectAtIndex:i];
        p.number = i;
        [dataArray addObject:p];
    }
}

- (void)initView{
    
    self.title = @"选择城市";
    
    //模拟数据加载 dataArray中得到Person的数组
    [self loadData];
    
    //根据Person对象的 name 属性 按中文 对 Person数组 排序
    self.indexArray = [BMChineseSort IndexWithArray:dataArray Key:@"name"];
    self.letterResultArr = [BMChineseSort sortObjectArray:dataArray Key:@"name"];
    
    _helpTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight() - 64)];
    _helpTableView.backgroundColor = getColor(@"ffffff");
    _helpTableView.delegate = self;
    _helpTableView.dataSource = self;
    _helpTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_helpTableView];
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(0.7))];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
}

#pragma mark -------------------- tableView ----------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
    NSString *text = [cell valueForKey:@"text"];// 获取点击的cell的text属性值
    NSLog(@"%@",text);
    
    for (int i = 0; i < _dataArr.count ; i ++) {
        
        if ([_dataArr[i][@"area_name"] isEqualToString:text]) {
            
            schoolChangeViewController *vc = [[schoolChangeViewController alloc]init];
            vc.city_id = _dataArr[i][@"city_id"];
            vc.parent_id = _dataArr[i][@"parent_id"];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
        
    }
    
}

//section的titleHeader
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.indexArray objectAtIndex:section];
}
//section行数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.indexArray count];
}
//每组section个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.letterResultArr objectAtIndex:section] count];
}
//section右侧index数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexArray;
}
//点击右侧索引表项时调用 索引与section的对应关系
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}
//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView registerClass:[cityListTableViewCell class] forCellReuseIdentifier:@"identifier"];
    cityListTableViewCell *cell = (cityListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //获得对应的Person对象<替换为你自己的model对象>
    Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = p.name;
    
    return cell;
}

@end
